import unittest

import requests
from parameterized import parameterized

from api.api_dlce import RegLoginApi
from api.api_cz import RechargeApi
from common.read_json import read_json
from common.断言参数化 import common_assert
from common.html解析 import html_util


class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        # 实例化登录模块封装的类
        self.reg_login = RegLoginApi(self.session)
        # 实例化充值模块封装的类
        self.re_charge = RechargeApi(self.session)

        self.reg_login.user_login('15666666111', '123456')

    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()

    # 获取充值验证码测试用例
    @parameterized.expand(read_json('test_cz','reacharge_verify_code'))
    def test01_get_recharge_verify_code(self,status_code,r):
        # 获取充值图片验证码
        response = self.re_charge.get_recharge_verify_code(r)
        # 断言
        self.assertEqual(status_code, response.status_code)

    # 充值测试用例
    @parameterized.expand(read_json('test_cz', 'reacharge'))
    def test02_recharge(self,amount,valicode,status_code,status,description):
        # 获取充值图片验证码
        self.re_charge.get_recharge_verify_code(123)
        # 充值
        response = self.re_charge.recharge(amount, valicode)
        # 断言
        common_assert(self, response, status_code, status, description)

    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 获取充值图片验证码
        self.re_charge.get_recharge_verify_code(123)
        # 充值
        response = self.re_charge.recharge('100000', '8888')
        data = response.text
        print(data)
        third_open = html_util(response)
        # 获取第三方开户接口的url
        url = third_open[0]
        # 获取第三方开户接口的请求体数据
        form_dict = third_open[1]
        # 获取第三方接口返回的响应结果
        response = self.re_charge.third_recharge(url, form_dict)
        data = response.text
        print(data)
        # 断言
        self.assertIn('OK', response.text)